package cn.kshost.zqyg.product.service.impl;

import cn.kshost.zqyg.model.dto.product.ProductDto;
import cn.kshost.zqyg.model.dto.product.SkuSaleDto;
import cn.kshost.zqyg.model.entity.product.Product;
import cn.kshost.zqyg.model.entity.product.ProductDetails;
import cn.kshost.zqyg.model.entity.product.ProductSku;
import cn.kshost.zqyg.model.vo.common.Result;
import cn.kshost.zqyg.model.vo.common.ResultCodeEnum;
import cn.kshost.zqyg.model.vo.h5.ProductItemVo;
import cn.kshost.zqyg.product.mapper.ProductDetailMapper;
import cn.kshost.zqyg.product.mapper.ProductMapper;
import cn.kshost.zqyg.product.mapper.ProductSkuMapper;
import cn.kshost.zqyg.product.service.ProductService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Override
    @Cacheable(value = "ProductSkuList",key = "'hotSale'")
    public List<ProductSku> getHotProductSkuList() {
        return productSkuMapper.selectHotProductSku();
    }

    @Override
//    @Cacheable(value = "SearchProductList",key = "#page + '-' + #limit + '-' + #productDto.hashCode()")
    public PageInfo<ProductSku> getProductList(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page, limit);
        List<ProductSku> productSkuList = productSkuMapper.selectAllProductSku(productDto);
        PageInfo<ProductSku> productSkuPageInfo = new PageInfo<>(productSkuList);
        return productSkuPageInfo;

    }

    @Override
    public ProductItemVo getProductItemDetailBySkuId(Integer skuId) {
        ProductItemVo productItemVo = new ProductItemVo();
        //根据skuId获取sku
         ProductSku productSku =  productSkuMapper.selectSkuById(skuId);
         productItemVo.setProductSku(productSku);
        //根据productId获取product信息
        Product product = productMapper.selectProductById(productSku.getProductId());
        productItemVo.setProduct(product);
        //分割product中轮播图字符串信息数组
        List<String> sliderUrlList = Arrays.asList(product.getSliderUrls().split(","));
        productItemVo.setSliderUrlList(sliderUrlList);
        //根据productId查询商品详情表分割数组
        ProductDetails productDetails =  productDetailMapper.selectDetailByProductId(product.getId());
        List<String> detailsImageUrlList = Arrays.asList(productDetails.getImageUrls().split(","));
        productItemVo.setDetailsImageUrlList(detailsImageUrlList);
        //序列化specValue 为jsonArray
        JSONArray specValueList = JSON.parseArray(product.getSpecValue());
        productItemVo.setSpecValueList(specValueList);
        //根据productID 遍历 查到的sku获取sku_spec 和id组成map
        List<ProductSku>  productSkuList=  productSkuMapper.selectSkuByProductId(product.getId());
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item ->{
            skuSpecValueMap.put(item.getSkuSpec(),item.getId());
        });
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);


        return productItemVo;
    }

    @Override
    public ProductSku getProductskuByid(Long skuId) {
        ProductSku productSku = productSkuMapper.selectSkuById(Integer.parseInt(skuId.toString()));
        return productSku;
    }
    @Transactional
    @Override
    public Boolean updateSkuSaleNumAndStock(List<SkuSaleDto> skuSaleDtos) {
        if(!CollectionUtils.isEmpty(skuSaleDtos)) {
            for(SkuSaleDto skuSaleDto : skuSaleDtos) {
                productSkuMapper.updateSale(skuSaleDto.getSkuId(), skuSaleDto.getNum());
            }
        }
        return true;
    }
}
